package cn.tedu.huntingmall.service.impl;

import cn.tedu.huntingmall.commons.exception.ServiceException;
import cn.tedu.huntingmall.mapper.UserMapper;
import cn.tedu.huntingmall.pojo.dto.user.UserLoginDTO;
import cn.tedu.huntingmall.pojo.dto.user.UserUpdatePasswordDTO;
import cn.tedu.huntingmall.pojo.dto.user.UserRegDTO;
import cn.tedu.huntingmall.pojo.entity.user.User;
import cn.tedu.huntingmall.pojo.vo.user.UserStandardVO;
import cn.tedu.huntingmall.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static cn.tedu.huntingmall.commons.response.ServiceCode.*;

@Service
@Slf4j
public class UserSerivceimpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Long insert(UserRegDTO userRegDTO) {
        log.debug("即将注册的用户信息为{}", userRegDTO);
        String username = userRegDTO.getUsername();
        UserStandardVO userInDB = userMapper.selectByUsername(username);
        if (userInDB != null) {
            log.error("注册失败,用户名{}已存在,请重新输入!", username);
            throw new ServiceException(CONFLICT, "注册失败,用户名已存在,请重新输入!");
        }

        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        LocalDateTime now = LocalDateTime.now();
        user.setGmt_create(now);
        user.setGmt_modified(now);

        log.debug("即将插入的数据信息为{}", user);
        int rows = userMapper.insert(user);
        if (rows != 1){
            log.error("服务器忙,请稍后再试!");
            throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后再试!");
        }
        return user.getId();
    }

    @Override
    public Long updatePassword(UserUpdatePasswordDTO userUpdatePasswordDTO) {
        String username = userUpdatePasswordDTO.getUsername();
        UserStandardVO user = userMapper.selectByUsername(username);
        if (user == null) {
            log.error("未找到{}对应的用户名,请重新输入!", username);
            throw new ServiceException(NOT_FOUND, "没有找到输入的用户名,请确认用户名!");
        }

        String password = userUpdatePasswordDTO.getPassword();
        if (!password.equals(user.getPassword())){
            log.error("输入的原始密码{}错误,请重新输入!", password);
            throw new ServiceException(NOT_FOUND, "原始密码错误,请重新输入!");
        }
        String passwordNew = userUpdatePasswordDTO.getPasswordNew();
        int rows = userMapper.updatePassword(username, passwordNew);
        if (rows != 1){
            log.error("服务器忙,请稍后重试!");
            throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后重试!");
        }
        return user.getId();
    }

    @Override
    public int login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        UserStandardVO user = userMapper.selectByUsername(username);
        if (user == null) {
            log.error("未找到{}对应的用户名,请重新输入!", username);
            throw new ServiceException(NOT_FOUND, "没有找到输入的用户名,请确认用户名!");
        }

        String password = userLoginDTO.getPassword();
        if (!password.equals(user.getPassword())){
            log.error("输入的原始密码{}错误,请重新输入!", password);
            throw new ServiceException(NOT_FOUND, "原始密码错误,请重新输入!");
        }
        return 1;
    }

}
